package com.diaohaiwen.framework.web.advice;

import cn.hutool.core.convert.Convert;
import com.diaohaiwen.common.exception.GlobalException;
import com.diaohaiwen.common.obj.Result;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.beans.PropertyEditorSupport;
import java.io.IOException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.Date;

import static com.diaohaiwen.common.enums.StatusCodeEnum.EXCEPTION;

@Configuration
@RestControllerAdvice("com.diaohaiwen")
public class GlobalAdvice {

    /**
     * date localTime localDate localDateTime 处理
     */
    @InitBinder
    protected void initBinder(WebDataBinder binder) {
        binder.registerCustomEditor(LocalDate.class, new PropertyEditorSupport() {
            @Override
            public void setAsText(String text) throws IllegalArgumentException {
                LocalDateTime localDateTime = Convert.toLocalDateTime(text);
                if (localDateTime != null) {
                    setValue(localDateTime.toLocalDate());
                } else {
                    setValue(null);
                }
            }
        });
        binder.registerCustomEditor(LocalDateTime.class, new PropertyEditorSupport() {
            @Override
            public void setAsText(String text) throws IllegalArgumentException {
                setValue(Convert.toLocalDateTime(text));
            }
        });
        binder.registerCustomEditor(LocalTime.class, new PropertyEditorSupport() {
            @Override
            public void setAsText(String text) throws IllegalArgumentException {
                LocalDateTime localDateTime = Convert.toLocalDateTime(text);
                if (localDateTime != null) {
                    setValue(localDateTime.toLocalTime());
                } else {
                    setValue(null);
                }
            }
        });
        binder.registerCustomEditor(Date.class, new PropertyEditorSupport() {
            @Override
            public void setAsText(String text) throws IllegalArgumentException {
                setValue(Convert.toDate(text));
            }
        });
    }

    // 自定义异常
    @ExceptionHandler(GlobalException.class)
    public Result<?> global(GlobalException e) {
        return unifiedProcessing(e, e.getStatus(), e.getMsg());
    }

    @ExceptionHandler(IOException.class)
    public Result<?> IO(GlobalException e) {
        return unifiedProcessing(e, EXCEPTION.status, EXCEPTION.describe);
    }

    @ExceptionHandler(Exception.class)
    public Result<?> IO(Exception e) {
        return unifiedProcessing(e, EXCEPTION.status, EXCEPTION.describe);
    }


    private Result<?> unifiedProcessing(Exception e, int status, String message) {
        e.printStackTrace();
        return Result.fail(status, e.getMessage());
    }

}
